package com.smile.weather.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.smile.weather.R;
import com.smile.weather.adapter.RecyclerAdapter_Day;
import com.smile.weather.adapter.RecyclerAdapter_Hour;
import com.smile.weather.bean.DayWeatherData;
import com.smile.weather.bean.HourWeatherData;
import com.smile.weather.interfaces.ISevenDayFragment;
import com.smile.weather.presenter.SevenDayPresener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by smile on 2016/8/23.
 */
public class SevenDayFragment extends BaseFragment implements ISevenDayFragment {

    private View mView;

    private RecyclerView rv_hour;
    private RecyclerAdapter_Hour mHourAdapter;
    private List<HourWeatherData> hourWeatherDatas;

    private RecyclerView rv_day;
    private RecyclerAdapter_Day mDayAdapter;
    private List<DayWeatherData> dayWeatherDatas;

    private SevenDayPresener sevenDayPresener;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {

        mView = inflater.inflate(R.layout.fragment_sevenday_weather, container, false);
        initView();

        return mView;
    }


    /**
     * 初始化视图(控件)
     */
    private void initView() {

        sevenDayPresener = new SevenDayPresener(getActivity());
        sevenDayPresener.setiSevenDayFragment(this);

        rv_hour = (RecyclerView) mView.findViewById(R.id.rv_hour);
        hourWeatherDatas = new ArrayList<>();
        mHourAdapter = new RecyclerAdapter_Hour(getActivity(), hourWeatherDatas);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL,
                false);
        rv_hour.setLayoutManager(layoutManager);
        rv_hour.setAdapter(mHourAdapter);

        rv_day = (RecyclerView) mView.findViewById(R.id.rv_day);
        dayWeatherDatas = new ArrayList<>();
        mDayAdapter = new RecyclerAdapter_Day(getActivity(), dayWeatherDatas);
        LinearLayoutManager layoutManager2 = new LinearLayoutManager(getActivity());
        rv_day.setLayoutManager(layoutManager2);
        rv_day.setAdapter(mDayAdapter);

    }

    @Override
    public void setHtml(String html) {
        Log.i("sdf", "html--" + html);
        sevenDayPresener.analyzeHtml(html);

    }

    @Override
    public void setHourWeather(List<HourWeatherData> hourWeather) {
        hourWeatherDatas.clear();
        hourWeatherDatas.addAll(hourWeather);
        mHourAdapter.notifyDataSetChanged();
        Log.i("sdf", "shw--更新完成" + hourWeatherDatas.size());
    }

    @Override
    public void setDayWeather(List<DayWeatherData> dayWeather) {
        dayWeatherDatas.clear();
        dayWeatherDatas.addAll(dayWeather);
        mDayAdapter.notifyDataSetChanged();
        Log.i("sdf", "sdw--更新完成" + dayWeatherDatas.size());
    }
}
